package bakudo.utils.toolTips {
	import flash.text.TextFieldAutoSize;	
	import flash.display.DisplayObject;
	import flash.display.Graphics;
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.geom.Point;
	import flash.text.TextField;
	import flash.text.TextFormat;
	import flash.text.TextFormatAlign;		

	/**
	 * @author Micha� Rabi�ski
	 */
	public class ToolTip extends Sprite {

		//::::::::::::::: public vars ::::::::::::::://
		public var tf : TextField = new TextField;

		//::::::::::::::: private vars ::::::::::::::://
		private static var _instance : ToolTip;

		private var created : Boolean;

		public static function create(stage : Stage) : void {
			if(!_instance)_instance = new ToolTip;
			if(!_instance.tf.parent) {
				var tf : TextField = _instance.tf;
				var tfo : TextFormat = new TextFormat();
				tfo.align = TextFormatAlign.LEFT;
				tfo.color = 0xFFFFFF;
				tfo.bold = true;
				tf.defaultTextFormat = tfo;
				tf.autoSize = TextFieldAutoSize.LEFT;
				_instance.addChild(tf);
			}
			if(!_instance.parent)stage.addChild(_instance);
			_instance.created = true;
		}

		public static function showAt(posx : Number,posy : Number,displayObject : DisplayObject,info : String) : void {
			if(_instance.created) {	
				var targetPoint : Point;
				var globalPoint : Point;
				targetPoint = new Point(posx, posy);
				globalPoint = displayObject.parent.localToGlobal(targetPoint);
				_instance.x = Math.round(globalPoint.x);
				_instance.y = Math.round(globalPoint.y);
				_instance.tf.text = info;
				_instance.showBackground();
			}
		}

		private function showBackground() : void {
			var g : Graphics = graphics;
			g.clear();
			g.beginFill(0x0000FF);
			g.drawRect(0, 0, tf.textWidth, tf.textHeight);
			g.endFill();
		}
	}
}
